package com.kotiln.lgc.lib_base.helper.`fun`

import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.net.Uri
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.kotiln.lgc.lib_base.R
import com.kotiln.lgc.lib_base.helper.`fun`.data.AppData
import com.kotiln.lgc.lib_base.util.LogUtil

/**
 *@fileName AppHelper
 *@date2021/2/1  10:06
 *@author AppHelper$
 *@describe TODO
 *@desc 应用帮助类
 */
object AppHelper {

    //上下文
    private lateinit var mContext: Context

    //包管理器
    private lateinit var pm: PackageManager

    //所有应用
    private val mAllAppList = mutableListOf<AppData>()

    //所有商店包名
    private lateinit var mAllMarkArray:Array<String>

    //分布View
    val mAllViewList= mutableListOf<View>()

    //初始化
    fun initHelper(mContext: Context) {
        this.mContext = mContext
        pm = mContext.packageManager

        loadAllApp()
    }

    //加载所有App
    fun loadAllApp() {
        val intent = Intent(Intent.ACTION_MAIN, null)
        intent.addCategory(Intent.CATEGORY_LAUNCHER)
        val appInfo: MutableList<ResolveInfo> = pm.queryIntentActivities(intent, 0)
        appInfo.forEachIndexed { _, resolveInfo ->
            val appData = AppData(
                resolveInfo.activityInfo.packageName,
                resolveInfo.loadLabel(pm) as String,
                resolveInfo.loadIcon(pm),
                resolveInfo.activityInfo.name,
                resolveInfo.activityInfo.flags == ApplicationInfo.FLAG_SYSTEM
            )
            mAllAppList.add(appData)
        }

        LogUtil.i("mAllappList:$mAllAppList")

        initPageView()

        //加载商店包名
        mAllMarkArray= mContext.resources.getStringArray(R.array.AppMarketArray)
    }

    //初始化PageView
    private fun initPageView() {
        for (i in 0 until getPageSize()) {
            //FrameLayout
            val rootView: ViewGroup =
                View.inflate(mContext, R.layout.layout_app_manager_item, null) as ViewGroup
            // 第一层 线性布局
            for (j in 0 until rootView.childCount) {
                //第二层 六个线性布局
                val childX: ViewGroup = rootView.getChildAt(j) as ViewGroup
                //第三层 四个线性布局
                for (k in 0 until childX.childCount) {
                    //第四层 两个View ImageView TextView
                    val child: ViewGroup = childX.getChildAt(k) as ViewGroup
                    val iv: ImageView = child.getChildAt(0) as ImageView
                    val tv: TextView = child.getChildAt(1) as TextView
                    //计算当前的下标
                    val index: Int = i * 24 + j * 4 + k
                    if(index < mAllAppList.size){
                        val data:AppData= mAllAppList[index]
                        tv.text = data.appName
                        iv.setImageDrawable(data.appIcon)
                        //点击事件
                        child.setOnClickListener {
                            intentApp(data.packageName)
                        }

                    }
                }
            }

            mAllViewList.add(rootView)

        }
    }

    //获取页面数量
    fun getPageSize(): Int {
        return mAllAppList.size / 24 + 1
    }

    //获取非系统应用
    fun getNotSystemApp():List<AppData>{
        return mAllAppList.filter { !it.isSystemApp }
    }

    //启动App
    fun launcherApp(appName: String): Boolean {
        if (mAllAppList.size > 0) {
            mAllAppList.forEach {
                if (it.appName == appName) {
                    intentApp(it.packageName)
                    return true
                }
            }
        }
        return false
    }

    //卸载App
    fun unInstallApp(appName: String): Boolean {
        if (mAllAppList.size > 0) {
            mAllAppList.forEach {
                if (it.appName == appName) {
                    intentUnInstallApp(it.packageName)
                    return true
                }
            }
        }
        return false
    }

    //跳转应用市场
     fun launcherAppStore(appName:String):Boolean{
        mAllAppList.forEach {
            if(mAllMarkArray.contains(it.packageName)){
                if(mAllAppList.size>0){
                    mAllAppList.forEach {data ->
                        if(data.appName == appName){
                            intentAppStore(data.packageName,it.packageName)
                            return true
                        }
                    }
                }
            }
        }


        return false
    }

    //启动App
    private fun intentApp(packName: String) {
        val intent: Intent? = pm.getLaunchIntentForPackage(packName)
        intent?.let {
            it.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            mContext.startActivity(it)
        }
    }

    //启动卸载App
    private fun intentUnInstallApp(packName: String) {
        val intent = Intent(Intent.ACTION_DELETE) //调用系统卸载
        intent.data = Uri.parse("package:$packName")
        intent.flags=Intent.FLAG_ACTIVITY_NEW_TASK
        mContext.startActivity(intent)
    }

    //跳转应用商店
    private fun intentAppStore(packageName: String, markPackageName: String) {
        val uri: Uri = Uri.parse("market://details?id=$packageName")
        val intent = Intent(Intent.ACTION_VIEW, uri)
        intent.setPackage(markPackageName)
        intent.flags=Intent.FLAG_ACTIVITY_NEW_TASK
        mContext.startActivity(intent)
    }

}